package com.xcity.game.achievement.helper;

import java.util.Collection;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementHelper;
import com.xcity.game.achievement.AchievementTemplate;

/**
 * 成就类型：员工品质<br/>
 * 1个参数：①员工数量②员工品质范围（n-m）
 * @author yang.li
 *
 */
public class EmployeeQualityHelper implements AchievementHelper {

	@Override
	public boolean update(Achievement refer, int... vals) {
		return isFinished(refer);
	}

	@Override
	public boolean isFinished(Achievement refer) {
		if (refer.getState() == State.INITED) {
			Player player = refer.getVm().getOwner();
			Collection<Employee> employees = player.getEmployees().getEmployees();
			int count = obtainCount(refer.getTemplate());
			int quality = obtainQuality(refer.getTemplate());
			int c = 0;
			for (Employee employee : employees) {
				if (employee.getTemplate().getQuality().ordinal() >= quality
						&& ++c >= count) {
					refer.setState(State.FINISHED);
					return true;
				}
			}
		}
		return false;
	}
	
	static int obtainCount(AchievementTemplate template) {
		return (int) template.getCondition(0);
	}
	
	static int obtainQuality(AchievementTemplate template) {
		return (int) template.getCondition(1);
	}
	
}
